package maurice.util;

import java.io.*;
import javax.swing.*;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class MP3 {
	private String filename;
	private Player player;
	private Thread threadPlay;
	private BufferedInputStream bis;
	private FileInputStream fis;

	// constructor that takes the name of an MP3 file
	public MP3(String filename) {
		this.filename = filename;
	}

	public void close() {
		if (player != null)
			player.close();
	}

	// play the MP3 file to the sound card
	public void play() {
		try {
			fis = new FileInputStream(filename);
			bis = new BufferedInputStream(fis);
			player = new Player(bis);

		} catch (Exception e) {
			System.out.println("Problem playing file " + filename);
			System.out.println(e);
		}

		// run in new thread to play in background
		threadPlay = new Thread() {
			public void run() {
				try {
					player.play();
				} catch (Exception e) {
					System.out.println(e);
				}

			}
		};
		threadPlay.setName("MP3thread");
		threadPlay.start();
	}


	public void stop() {

		if (player != null) {
			player.close();
			player = null;
		}
	}
	
	public boolean isComplete(){
		if (player == null)
			return true;
		
		return player.isComplete();
	}

	public static void main(String[] args) {
		MP3 mp3 = new MP3("C:\\Users\\RICHTER Romain\\Desktop\\sons\\up_up.mp3");
		// MP3 mp3bis = new
		// MP3("C:\\Users\\RICHTER Romain\\Documents\\workspace\\danseavecmaurice\\ressources\\sons\\Boucle_Tropic.mp3");
		MP3 mp3bis = new MP3(
				"C:\\Users\\RICHTER Romain\\Documents\\workspace\\danseavecmaurice\\ressources\\musiques\\Alpha beat - Fascination.mp3");

		System.out.println("test");
		// mp3.waitEnd();
		// mp3bis.play();
	}
}
